package com.example.mqltest.service.serviceImp;


import com.example.mqltest.Dto.WorkFileDto;
import com.example.mqltest.dao.CourseDao;
import com.example.mqltest.dao.StudentDao;
import com.example.mqltest.dao.TeacherDao;
import com.example.mqltest.dao.WorkDao;
import com.example.mqltest.entity.*;

import com.example.mqltest.service.AccountService;
import com.example.mqltest.service.CourseService;
import com.example.mqltest.util.SimpleFormatDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CourseServiceImp  implements CourseService {
    @Autowired
    private CourseDao courseDao;

    @Autowired
    TeacherServiceImp teacherServiceImp;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    AccountService accountService;
    @Autowired
    private WorkDao workDao;

    @Override
    public List<Course> getCourseByStudent(String studentId) {
        List<Course> courseList=courseDao.getCourseByStudent( studentId );
        for (Course course:courseList
             ) {
            //查出班级的学生
            course.setStudentList( studentDao.getStudentByCode( course.getCode() ) );
            //查出班级的老师
            course.setTeacherList( teacherDao.getTeacherByCode( course.getCode() ) );
            //查出班级的作业（待确定)
            course.setWorkList( workDao.getWorkByCode( course.getCode() ) );
        }
        return courseList;
    }

    @Override
    public List<Course> getCourseByTeacher(String teacherId) {
        List<Course> courseList=courseDao.getCourseByTeacher( teacherId );
        for (Course course:courseList
        ) {
            //查出班级的学生
            course.setStudentList( studentDao.getStudentByCode( course.getCode() ) );
            //查出班级的老师
            course.setTeacherList( teacherDao.getTeacherByCode( course.getCode() ) );
            //查出班级的作业（待确定)
           //查出班级的作业（待确定)
            course.setWorkList( workDao.getWorkByCode( course.getCode() ) );
        }
        return courseList;
    }

    @Override
    public Result createCourse(Course course) {
        course.setCode( generateCode() );
        course.setCreateDate( new Date() );
        course.setDescribe( "无" );
        try {
            courseDao.createGroup(course);
            return new Result(200,"创建课程成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙","");
        }
    }

    //归档
    @Override
    public Result archiveCourse(String code, String studentId, String teacherId,String archiveStatus) {
        String errorMessage="";
        String successMessage="";
        if(archiveStatus.equals("true")){
            errorMessage="网络繁忙，归档失败";
            successMessage="归档成功";
        }else {
            errorMessage="网络繁忙，恢复失败";
            successMessage="恢复成功";
        }
        try {
            if(studentId!=null){
                studentDao.archiveCourse(code,studentId,archiveStatus);
            }
            if(teacherId!=null){
                teacherDao.archiveCourse(code,teacherId,archiveStatus);
            }
            return new Result(200,successMessage,"");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,errorMessage,"");
        }
    }

    //获取学生交的文件
    @Override
    public List<WorkFileDto> getWorkFileByStudent(String studentId, int workId) {
        return courseDao.getWorkFileByStudent( studentId,workId );
    }

    @Override
    public void studentUpdateWorkStatus(String studentId, int workId, String status, Date date) {
        courseDao.studentUpdateWorkStatus(studentId,workId,status,date);
    }

    @Override
    public Work getWorkById(int workId) {
        return workDao.getWorkById(workId);
    }

    @Override
    public Result deleteWorkFile(int fileId) {
        try {
            workDao.deleteWorkFile(fileId);
            return new Result(200,"删除成功","");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(400,"网络繁忙，删除失败","");
        }
    }

    @Override
    public List<StudentWork> getStudentWorkById(int workId) {
        return courseDao.getStudentWorkById(workId);
    }

    @Override
    public List<Work> getAllWorksByCode(String code) {
        List<Work> works=workDao.getWorkByCode( code );

        for (Work work:works
             ) {
            List<String> s=workDao.getNum(work.getId(),"待批改");
            s.addAll( workDao.getNum( work.getId(),"已批改" ) );
            work.setSubmittedStudentIds(s);
            work.setTotalStudentIds(workDao.getNum(work.getId(),""));
        }
        return works;
    }

    @Override
    public WorkList getStudentWork(int workId, String studentId) {
        return workDao.getStudentWork(workId,studentId);
    }

    public String generateCode() {
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        String newCode = "";
        boolean flag = false;
        do {
            flag = false;
            for (int i = 0; i < 6; i++) {
                int ran2 = (int) (Math.random() * 35);
                newCode += str[ran2];
            }
            List<String> codeList = new ArrayList<String>();
            codeList = courseDao.codeList();
            for (int i = 0; i < codeList.size(); i++) {
                if (newCode.equals( codeList.get( i ) )) {
                    flag = true;
                    newCode = "";
                }
            }
        } while (flag);
        return newCode;
    }
}
